package com.liujiajin;

import java.net.URL;
import java.net.URLClassLoader;

public class CustomClassLoader extends URLClassLoader {
	public CustomClassLoader(URL[] urls) {
		super(urls, null);
	}

	@Override
	public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
		synchronized (getClassLoadingLock(name)) {
			// 首先检查是否已经加载了该类
			Class<?> loadedClass = findLoadedClass(name);
			if (loadedClass != null) {
				return loadedClass;
			}
            // 尝试从父类加载器加载类
            try {
                loadedClass = super.loadClass(name, resolve);
            } catch (ClassNotFoundException e) {
                // 父类加载器无法加载该类，尝试从当前类加载器加载
                loadedClass = findClass(name);
            }

			if (resolve) {
				resolveClass(loadedClass);
			}

			return loadedClass;
		}
	}
}